import cv2
import numpy as np


def test():
    #
    img = cv2.imread('lina.png', cv2.IMREAD_GRAYSCALE)

    # Canny 边缘检测
    threshold1 = 50  # 较小的阈值
    threshold2 = 200  # 较小的阈值
    apertureSize = 1  #
    L2gradient = 11  #
    edges = cv2.Canny(img, threshold1, threshold2)
    print('>>>>>> ', len(edges))

    #
    cv2.imshow('edges-canny', edges)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


def test2():
    #
    img = cv2.imread('lina.png', cv2.IMREAD_GRAYSCALE)

    # 应用Canny边缘检测
    edges1 = cv2.Canny(img, threshold1=100, threshold2=200)
    edges2 = cv2.Canny(img, threshold1=0, threshold2=50)

    # 显示结果
    cv2.imshow('Edges1', edges1)
    cv2.imshow('Edges2', edges2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


def test3():
    #
    img = cv2.imread('lina.png')

    # 灰度
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 边缘检测
    edges = cv2.Canny(gray, 100, 200)
    cv2.imshow('src1', gray)

    # 空白掩膜
    mask = np.zeros_like(edges)
    cv2.imshow('src2', mask)
    #
    mask[edges >= 0] = 255
    cv2.imshow('src3', mask)
    # 对原始图进行抠图
    result = cv2.bitwise_and(img, img, mask=mask)

    cv2.imshow('src', img)
    cv2.imshow('Edges2', result)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
